/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DBLayer;

import DomainLayer.Zip;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Rose
 */
public class ZipDB implements ZipIFDB {
    private Connection con;

      /** Creates a new instance
     * of ZipDB */

    public ZipDB(){
        con = ConnectorDB.getInstance().getConDB();
    }
      //Implements the methods from the interface

    public int createZip(Zip zip) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Zip readZip(int zipCode, boolean retriveAssociation) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int zip(int zipCode) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public ArrayList getAllZips(boolean retriveAssociation) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    private String buildQuery(String wClause)
    {
        String query = "SELECT ZipCode,City";
        if( wClause.length()>0)
            query = query + "WHERE" + wClause;

        return query;
    }

    private Zip buildZip (ResultSet results)
    {
       Zip zipObj = new Zip();
       try{
           zipObj.setZipCode(results.getInt("ZipCode"));
           zipObj.setCity(results.getString("City"));
       }
       catch (Exception e)
       {
           System.out.println(e.getMessage());
       }
       return zipObj;
    }
    //private methods
        //michWere is used whenever we want to select more than one zip
    private ArrayList<Zip> miscWhere(String wClause, boolean retrieveAssociation) throws SQLException
    {
        ResultSet results;
        ArrayList<Zip> list = new ArrayList <Zip>();

        String query = buildQuery(wClause);

        try {// read the customer from the database
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            int snr =0; //WHAT IS THIS????
            while ( results.next()){
                Zip zipObj = new Zip();
                zipObj = buildZip(results);
                list.add(zipObj);
            }
            stmt.close();
            return list;
        }

        catch (Exception e)
        {
            System.out.println( e.getMessage());
            return null;
        }
    }
    //Singelwhere is used when we only select one Zip
    private Zip singleWhere(String wClause, boolean retrieveAssociation)
    {
        ResultSet results;
        Zip zipObj = new Zip();

        String query = buildQuery (wClause);
        System.out.println(query);
        try{ // read the zip from database
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);
            if( results.next()){
                zipObj = buildZip(results);
            }
               // associate is to be build
            stmt.close();
            return zipObj;
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            return null;
        }
    }

}
